//
// Actor.cs
//
// Copyright (C) 2008 Lukas Lipka <lukaslipka@gmail.com>
//

using System;
using System.Collections.Generic;

namespace Dashboard.Mint.Theatrics {

	public class Actor<T> {

		private T role = default (T);

		private DateTime start;
		private uint delay = 0;
		private uint duration = 0;

		private double percent = 0;
		private double frames = 0;

		public delegate void ActorSteppedDelegate (T role);
		public event ActorSteppedDelegate Stepped;

		public Actor (T role, uint duration, uint delay)
		{
			this.role = role;
			this.duration = duration;
			this.delay = delay;
		}

		public Actor (T role, uint duration) : this (role, duration, 0)
		{
		}

		public void Reset ()
		{
			this.percent = 0;
			this.frames = 0;
			this.start = DateTime.Now;
		}

		public void Step ()
		{
			double elapsed = (DateTime.Now - start).TotalMilliseconds;

			if (elapsed < delay)
				return;

			this.percent = (elapsed - delay) / duration;
			this.frames++;

			if (Stepped != null)
				Stepped (role);
		}

		public T Role {
			get { return role; }
		}

		public bool Expired {
			get { return percent >= 1.0; }
		}

		public double Percent {
			get { return Math.Max (0.0, Math.Min (percent, 1.0)); }
		}

		public uint Delay {
			get { return delay; }
		}

		public double Frames {
			get { return frames; }
		}
	}
}
